package com.example.shopping_li.goods;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.shopping_li.R;
import com.example.shopping_li.URLUtils;
import com.example.shopping_li.cart.PayActivity;
import com.example.shopping_li.me.UserBean;
import com.example.shopping_li.utils.BaseActivity;
import com.example.shopping_li.utils.BuyDialog;
import com.example.shopping_li.utils.SaveUserUtils;
import com.google.gson.Gson;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GoodsInfoActivity extends BaseActivity implements View.OnClickListener {
    TextView nameTv,priceTv,desTv,numTv;
    Button buyBtn,cartBtn;
    ImageView backIv,bigIv;
    GridView likeGv;
    List<GoodsTypeBean.DataBean> data;//数据源
    LikeAdapter adapter;
    GoodsTypeBean.DataBean bean;
    UserBean.DataBean userInfo;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods_info);
        initView();
        userInfo = getUserInfo();
        //接收上一个页面传来的数据
         bean= (GoodsTypeBean.DataBean) getIntent().getSerializableExtra("info");
    showView(bean);
    connectNet(URLUtils.guessLikeURL);
    setItemClick();
    }

    private void setItemClick() {
        likeGv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                GoodsTypeBean.DataBean bean = data.get(position);
                Intent intent=new Intent(GoodsInfoActivity.this,GoodsInfoActivity.class);
                intent.putExtra("info",bean);
                startActivity(intent);

            }
        });
    }

    private void showView(GoodsTypeBean.DataBean bean) {
        nameTv.setText(bean.getName());
        priceTv.setText("￥"+bean.getPrice()+"元");
        desTv.setText(bean.getInfo());
        numTv.setText("剩余："+bean.getCount()+"件");
        String picUrl= URLUtils.PUBLIC_URL+bean.getPhoto();
        Picasso.with(this).load(picUrl).into(bigIv);
//展示GridView的内容
        data=new ArrayList<>();
//适配器
        adapter=new LikeAdapter(this,data);
        likeGv.setAdapter(adapter);
    }
    @Override
    public void onSuccess(String result) {
        GoodsTypeBean bean=new Gson().fromJson(result,GoodsTypeBean.class);
        if (bean.getData()==null) {
            if (bean.getCode()==200) {
                Toast.makeText(this, "添加购物车成功了", Toast.LENGTH_SHORT).show();
            }
        }else {
            List<GoodsTypeBean.DataBean> list = bean.getData();
            data.addAll(list);
            adapter.notifyDataSetChanged();
        }
    }
    private void initView() {
        nameTv = findViewById(R.id.info_tv_name);
        priceTv = findViewById(R.id.info_tv_price);
        desTv = findViewById(R.id.info_tv_des);
        numTv = findViewById(R.id.info_tv_num);
        buyBtn = findViewById(R.id.info_btn_buy);
        cartBtn = findViewById(R.id.info_btn_cart);
        backIv = findViewById(R.id.info_iv_back);
        bigIv = findViewById(R.id.info_iv_goods);
        likeGv = findViewById(R.id.info_gv_like);
        //设置点击事件
        buyBtn.setOnClickListener(this);
        cartBtn.setOnClickListener(this);
        backIv.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.info_btn_buy:
                if (userInfo==null) {
                    Toast.makeText(this, "购买之前请先登录！", Toast.LENGTH_SHORT).show();
                }else{
                    showBuyDialog(0);
                }

                break;
            case R.id.info_btn_cart:
                if (userInfo==null) {
                    Toast.makeText(this, "购买之前请先登录！", Toast.LENGTH_SHORT).show();
                }else{
                    showBuyDialog(1);
                }
                break;
            case R.id.info_iv_back:
                finish();//销毁当前页面
                break;

        }
    }
    private UserBean.DataBean getUserInfo() {
        UserBean.DataBean bean = SaveUserUtils.getUserinfo();
        if (bean == null) {
            bean = SaveUserUtils.getUserToObj(this);

        }
        return bean;
    }
    private void showBuyDialog(int flag) {
        BuyDialog dialog = new BuyDialog(this);
        dialog.show();
        dialog.setDialogWidth();
        dialog.setOnEnsureListener(new BuyDialog.OnEnsureListener() {
            @Override
            public void onEnsure(int count) {
                Intent intent=new Intent();
                if (flag==0) {//跳转到支付页面
                   // intent.setClass(GoodsInfoActivity.this);
                    intent.setClass(GoodsInfoActivity.this, PayActivity.class);
                    intent.putExtra("flag",0);//标志，直接购买跳转购买
                    intent.putExtra("count",count);//购买数量
                    intent.putExtra("info",bean);
                    startActivity(intent);
                }else if (flag==1){//跳转到购物车页面

                    //加入购物车
                    Map<String,String>map=new HashMap<>();
                    map.put("userId",userInfo.getId()+"");
                    map.put("commodityId",bean.getId()+"");
                    map.put("count",count+"");
                    postNetwork(URLUtils.addCartURL,map);
dialog.cancel();
                }
            }
        });
    }

}